<?php

class QuestionnaireModel extends Model{
//--------------------------------------insert-------------------------------------------------------------
    public function add_questionnaire($data)
    {
        $data['questionnaire_end_time'] = strtotime($data['questionnaire_end_time']);
        return M('questionnaire','','DB_MEETING')->add($data);
    }

    public function add_question($data)
    {
        if($data['question_type'] == 3)
            return M('questionnaire_question','','DB_MEETING')->add($data);
        else{
            $rs =  M('questionnaire_question','','DB_MEETING')->add($data);
            $i='A';

            foreach($data['answer'] as $val)
            {
                if($val == '')
                    break;
                else{
                    $data1['question_id'] = $rs;
                    $data1['opt']         = $i;
                    $data1['answer']      = $val;
                    M('questionnaire_question_opt','','DB_MEETING')->add($data1);
                    $i++;
                }
            }
            return $rs;
        }
    }
//--------------------------------------update-------------------------------------------------------------
    public function upt_questionnaire($data)
    {
        dump($data);
    }

    public function update_state($data)
    {
        return M('questionnaire','','DB_MEETING')->where('questionnaire_id='.$data['questionnaire_id'])->save($data);
    }

    public function upt_question($data)
    {
        return M('questionnaire_question','','DB_MEETING')->where('question_id='.$data['question_id'])->save($data);
    }
//--------------------------------------delete-------------------------------------------------------------
    public function del_questionaire($questionaire_id)
    {
        return M('questionnaire_question','','DB_MEETING')->where('questionaire_id='.$questionaire_id)->delete();
    }

    public function del_question($question_id)
    {
        return M('questionnaire_question','','DB_MEETING')->where('question_id='.$question_id)->delete();
    }
//--------------------------------------select-------------------------------------------------------------
    public function get_question_num($questionnaire_id)
    {
        $rs = M('questionnaire_question','','DB_MEETING')->field('questionnaire_id')->where('questionnaire_id='.$questionnaire_id)->select();
        return count($rs);
    }

    public function get_audience_num($questionnaire_id)
    {
        $rs = M('questionnaire_audience_answer','','DB_MEETING')->field('questionnaire_id')->where('questionnaire_id='.$questionnaire_id)->group('audience_id')->select();
        return count($rs);
    }

    public function get_questions($data)
    {   

        $rs = M('questionnaire_question','','DB_MEETING')->where('questionnaire_id='.$data)->select();
        $count = count($rs);
        for ($i=0;$i<$count;$i++) {
            $rs[$i]['num'] = $i+1;
        }
        return $rs;
    }
        
    public function get_audience_answer_num($rs){
        for($i=0;$i<count($rs);$i++){
            //总票数
            $count  = M('questionnaire_audience_answer','','DB_MEETING')->where('question_id='.$rs[$i]['question_id'])->count();
            $rs[$i]['audience_num'] = $count;
            $rs[$i]['num'] = $i+1;
        }
        return $rs;
    }

    public function get_detail($data){
        $arr = [];
        $c = count($data);
        for($i=0;$i<$c;$i++){
            $sql = 'select count(answer) as num,answer as opt from questionnaire_audience_answer where question_id='.$data[$i]['question_id'].' GROUP by answer';
            $rs = M('questionnaire_audience_answer','','DB_MEETING')->query($sql);
            
            if($rs){
                foreach($rs as $val){
                    $val['opt'] == null && $val['opt'] = 'other';
                    $val['percent'] = (round(($val['num'] / $data[$i]['audience_num']),2)*100).'%';
                    $arr[] = array('question_id' => $data[$i]['question_id'],'opt'=>$val['opt'],'num'=>$val['num'],'percent'=>$val['percent']);
                }
            }else{
                $sql = 'select opt from questionnaire_question_opt where question_id='.$data[$i]['question_id'].' GROUP by opt';
                $re = M('questionnaire_question_opt','','DB_MEETING')->query($sql);
           
                foreach ($re as $value) {
                     $arr[] = array('question_id' => $data[$i]['question_id'],'opt'=>$value['opt'],'num'=>'0','percent'=>'0');
                }
            }
        }
       
        return $arr;
    }
}